package com.vipheyue.uiautomator;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;
import android.view.View;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Random;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    /**
     * 运行这个单元测试就行了*/
    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.vipheyue.uiautomator", appContext.getPackageName());
    }

    private static final String BASIC_SAMPLE_PACKAGE = "com.p1.mobile.putong";
    //    private static final String BASIC_SAMPLE_PACKAGE = "com.vipheyue.uiautomator";
    private static final int LAUNCH_TIMEOUT = 5000;
    private static final String STRING_TO_BE_TYPED = "UiAutomator";
    private UiDevice mDevice;

    @Before
    public void startMainActivityFromHomeScreen() {
        // Initialize UiDevice instance
        mDevice = UiDevice.getInstance(getInstrumentation());

        // Start from the home screen
        mDevice.pressHome();

        // Wait for launcher
        final String launcherPackage = mDevice.getLauncherPackageName();
        assertThat(launcherPackage, notNullValue());
        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
                LAUNCH_TIMEOUT);

        // Launch the app
        Context context = InstrumentationRegistry.getContext();
        final Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
        // Clear out any previous instances
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);

        // Wait for the app to appear
        mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
                LAUNCH_TIMEOUT);
        tanTanClick();

    }

/*    @Test
    public void findUI() throws UiObjectNotFoundException {
        UiObject cancelButton = mDevice.findObject(new UiSelector()
                .text("123"));
        if (cancelButton.exists()) {
            cancelButton.click();
        }

    }*/

 /*   @Test
    public void findUITanTan() throws UiObjectNotFoundException {
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                tanTanClick();
            }
        });

    }*/


    private void tanTanClick() {
        UiObject cancelButton = mDevice.findObject(new UiSelector()
                .instance(1)
                .className(View.class)
                .enabled(true)
                .focusable(true)
                .clickable(true));
        if (cancelButton.exists()) {
            try {
                cancelButton.click();
            } catch (UiObjectNotFoundException e) {
                e.printStackTrace();
            }
        }
        try {
            int sleepTime = new Random().nextInt(4000);
            Thread.sleep(sleepTime);
            tanTanClick();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

 /*   @Test
    public void findUIById() throws UiObjectNotFoundException {
        UiObject2 cancelButton = mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "btn_test"));
        cancelButton.click();
    }*/
}
